<?php

namespace services\marketing;

use services\common\CommonService;
use Yii;
use common\components\Service;
use common\models\marketing\OutpatientType;

/**
 * Class OutpatientTypeService
 * @package services\marketing
 */
class OutpatientTypeService extends Service
{
    /**
     * 根据ID查找信息
     *
     * @param int $id
     * @return OutpatientType|null
     */
    public function findById(int $id = 0): ?OutpatientType
    {
        return OutpatientType::find()->where(['id' => $id])->one();
    }

    /**
     * 查找信息
     *
     * @param array $condition
     * @return OutpatientType|null
     */
    public function findInfo(array $condition = []): ?OutpatientType
    {
        $query = OutpatientType::find();

//        isset($condition['project_id']) && $query->andWhere(['project_id' => $condition['project_id']]);
//        isset($condition['name']) && $query->andWhere(['name' => $condition['name']]);
//        isset($condition['status']) && $query->andWhere(['status' => $condition['status']]);

        return $query->one();
    }

    /**
     * 查找列表
     *
     * @param array $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    public function findList(array $condition = [])
    {
        $query = OutpatientType::find();

        // select
        isset($condition['select']) && $query->select($condition['select']);

        // condition
        isset($condition['id']) && $query->andWhere(['id' => $condition['id']]);
        // isset($condition['project_id']) && $query->andWhere(['project_id' => $condition['project_id']]);
        isset($condition['status']) && $query->andWhere(['status' => $condition['status']]);

        return $query->asArray()->all();
    }


    /**
     * 就诊类型列表
     *
     * @return array
     */
    public function getList()
    {
        $list = OutpatientType::find()->select('id, outpatient_name, status, is_inhibit_delete')
            ->where(['status' => [OutpatientType::STATUS_INVALID, OutpatientType::STATUS_ENABLE]])
            ->orderBy(['status' => SORT_DESC, 'sort' => SORT_ASC])
            ->asArray()
            ->all();

        return [
            'list' => $list,
        ];
    }

    /**
     * 新增就诊类型
     *
     * @param $params
     * @return array
     */
    public function create($params)
    {
        $exists = $this->checkName([
            'outpatient_name' => $params['outpatient_name'],
            'status' => [OutpatientType::STATUS_ENABLE, OutpatientType::STATUS_INVALID]
        ]);
        if ($exists) {
            return ['code' => 422, 'message' => '该就诊类型名称已存在'];
        }

        $model = new OutpatientType();
        $model->outpatient_name = $params['outpatient_name'];
        $model->status = $params['status'];
        $model->is_inhibit_delete = OutpatientType::INHIBIT_DELETE;
        $model->operation_id = $params['member_id'];
        if (!$model->save()) {
            return ['code' => 422, 'message' => '新增失败'];
        }

        // 删除缓存
        CommonService::baseRedis('out_patient_type', 2);

        return ['code' => 200, 'message' => '新增成功'];
    }

    /**
     * 判断是否存在
     *
     * @param $where
     * @return bool
     */
    public function checkName($where)
    {
        return OutpatientType::find()->where($where)->exists();
    }

    /**
     * 编辑状态
     *
     * @param int $id
     * @param int $status
     * @return array
     */
    public function updateStatus(int $id, int $status)
    {
        $exists = $this->checkName(['id' => $id]);
        if (!$exists) {
            return ['code' => 422, 'message' => '该就诊类型不存在'];
        }
        $data = OutpatientType::updateAll(['status' => $status], ['id' => $id]);
        if (!$data) {
            return ['code' => 422, 'message' => '修改失败'];
        }
        // 删除缓存
        CommonService::baseRedis('out_patient_type', 2);

        return ['code' => 200, 'message' => '修改成功'];
    }

    /**
     * 编辑就诊类型
     *
     * @param array $params
     * @return array
     */
    public function update(array $params)
    {
        $exists = $this->checkName(['id' => $params['id']]);
        if (!$exists) {
            return ['code' => 422, 'message' => '数据不存在'];
        }

        $model = OutpatientType::findOne($params['id']);
        $outpatientTypeIs = OutpatientType::findOne(
            [
                'outpatient_name' => $params['outpatient_name'],
                // 'project_id' => $params['project_id'],
                'status' => [OutpatientType::STATUS_ENABLE, OutpatientType::STATUS_INVALID],
            ]
        );
        if ($outpatientTypeIs && $outpatientTypeIs->id != $params['id']) {
            return ['code' => 422, 'message' => '该就诊类型名称已存在'];
        }

        $model->outpatient_name = $params['outpatient_name'];
        // $model->project_id = $params['project_id'];
        $model->status = $params['status'];
        $model->is_inhibit_delete = OutpatientType::INHIBIT_DELETE;
        $model->operation_id = $params['member_id'];
        if ($model->save() == false) {
            return ['code' => 422, 'message' => '修改失败'];
        }

        // 删除缓存
        CommonService::baseRedis('out_patient_type', 2);

        return ['code' => 200, 'message' => '修改成功'];
    }

    /**
     * 删除就诊类型
     *
     * @param int $id
     * @return array
     */
    public function delete(int $id)
    {
        $exists = $this->checkName(['id' => $id]);
        if (!$exists) {
            return ['code' => 422, 'message' => '该就诊类型不存在'];
        }
        $data = OutpatientType::updateAll(['status' => OutpatientType::STATUS_DELETE], ['id' => $id]);
        if (!$data) {
            return ['code' => 422, 'message' => '删除失败'];
        }
        // 删除缓存
        CommonService::baseRedis('out_patient_type', 2);

        return ['code' => 200, 'message' => '删除成功'];
    }

}
